package com.hnly.djdl.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBackIos
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.accompanist.insets.systemBarsPadding
import com.hnly.djdl.theme.MainBackgroundColor
import com.hnly.djdl.theme.SubTitleColor
import com.hnly.djdl.theme.TitleColor


@Composable
fun TitlePage(
    upPress: () -> Unit = {},
    onSaveClick: () -> Unit = {},
    centerTitle: String = "中间标题",
    rightTitle: String = "保存",
    content: @Composable ColumnScope.() -> Unit,
) {
    CommonTitlePage(leftContent = {
        Icon(
            imageVector = Icons.Filled.ArrowBackIos,
            contentDescription = null,
            modifier = Modifier.clickable(onClick = upPress),
            tint = SubTitleColor
        )
    }, centerContent = {
        Text(
            text = centerTitle,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            style = TextStyle.Default.copy(
                fontSize = 26.sp, fontWeight = FontWeight.Bold, color = TitleColor
            )
        )
    }, rightContent = {
        TextButton(onClick = onSaveClick) {
            Text(
                text = rightTitle, style = TextStyle.Default.copy(color = SubTitleColor)
            )
        }
    }, content = content)
}

@Composable
fun CommonTitlePage(
    leftContent: @Composable RowScope.() -> Unit = {
        Icon(
            imageVector = Icons.Filled.ArrowBackIos, contentDescription = null, tint = SubTitleColor
        )
    },
    centerContent: @Composable RowScope.() -> Unit = {
        Text(
            text = "中间Center",
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            style = TextStyle.Default.copy(
                fontSize = 26.sp, fontWeight = FontWeight.Bold, color = TitleColor
            )
        )
    },
    rightContent: @Composable RowScope.() -> Unit = {
        TextButton(onClick = {}) {
            Text(
                text = "保存", style = TextStyle.Default.copy(color = SubTitleColor)
            )
        }
    },
    content: @Composable ColumnScope.() -> Unit,
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(MainBackgroundColor)
            .systemBarsPadding(true)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 12.dp)
                .height(58.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            leftContent()
            centerContent()
            rightContent()
        }
        content()
    }
}

@Preview
@Composable
fun TitlePagePreview() {
    TitlePage(content = {})
}